{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(r\"C:\\mySpace\\DEVTEST\\stocksfuture\\jue_jin_strategy\\python_quant\")\n",
    "from common.date import date_format,date_to_str\n",
    "import akshare as ak\n",
    "import datetime\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "获取全市场的涨停板"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "stock_zt_pool_em_df = ak.stock_zt_pool_em(date='20210525')\n",
    "stock_zt_pool_em_df"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "当天9:31分之前涨停的股票"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "zt_pool_before_931_em = stock_zt_pool_em_df[stock_zt_pool_em_df['首次封板时间'] <= '0931']\n",
    "zt_pool_before_931_em"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "9:31分之前涨停的股票中筛选"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "全市场第一个涨停板"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "top_1_zt = zt_pool_before_931_em.head(1)\n",
    "top_1_zt"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "top 1 必须是换手板,中间有开板,二板"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "floor_2_zt = top_1_zt[(top_1_zt['连板数']==2) & (top_1_zt['炸板次数']>0)]\n",
    "floor_2_zt"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "按交易日列表，筛选top 1 必须是换手板,中间有开板,二板的股票"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "获取交易日列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "stock_zh_index_daily_df = ak.stock_zh_index_daily(symbol=\"sh000001\")\n",
    "stock_zh_index_daily_df"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "交易日列表匹配东方财富涨停板行情数据段"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "start_date = datetime.datetime.strptime('2020-01-04', '%Y-%m-%d').date()\n",
    "easymoney_dates = stock_zh_index_daily_df[(stock_zh_index_daily_df['date']>=start_date)]['date'].values\n",
    "easymoney_dates"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "循环获取所有满足条件的股票\n",
    "todo 连板次数应该结合炸板池统一计算，要考虑炸板没有回封成功的票"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "floor_2_conditon_pool = pd.DataFrame()\n",
    "for easymoney_date in easymoney_dates:\n",
    "    start_date = date_format(date_to_str(easymoney_date, '%Y-%m-%d'), '%Y-%m-%d', '%Y%m%d')\n",
    "    stock_zt_pool_em_df = ak.stock_zt_pool_em(date=start_date)\n",
    "    if len(stock_zt_pool_em_df) == 0:\n",
    "        continue\n",
    "    zt_pool_before_931_em = stock_zt_pool_em_df[stock_zt_pool_em_df['首次封板时间'] <= '1030']\n",
    "    top_1_zt = zt_pool_before_931_em.head(1)\n",
    "    floor_2_zt = top_1_zt[(top_1_zt['连板数']==2) & (top_1_zt['炸板次数']>0)]\n",
    "    # 加入当日炸板池\n",
    "    stock_zt_pool_zbgc_em_df = ak.stock_zt_pool_zbgc_em(date=start_date)\n",
    "    zbgc_1030 = stock_zt_pool_zbgc_em_df[(stock_zt_pool_zbgc_em_df['首次封板时间']< '1030') & (stock_zt_pool_zbgc_em_df['涨停统计']== '2/1')]\n",
    "\n",
    "    zbgc_floor2_1030 = zbgc_1030[['代码', '首次封板时间']]\n",
    "    zt_floor2_1030 = floor_2_zt[['代码', '首次封板时间']]\n",
    "\n",
    "    conditon_pool = pd.concat([zt_floor2_1030, zbgc_floor2_1030])\n",
    "    conditon_pool['日期'] = start_date\n",
    "    floor_2_conditon_pool = pd.concat([conditon_pool, floor_2_conditon_pool])\n",
    "\n",
    "\n",
    "floor_2_conditon_pool.sort_values('日期')"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "获取炸板股池"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "stock_zt_pool_zbgc_em_df = ak.stock_zt_pool_zbgc_em(date=start_date)\n",
    "zbgc_1030 = stock_zt_pool_zbgc_em_df[(stock_zt_pool_zbgc_em_df['首次封板时间']< '1030') & (stock_zt_pool_zbgc_em_df['涨停统计']== '2/1')]\n",
    "zbgc_1030[['代码', '首次封板时间']]"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
